import logging

import pytest

from resource.pom.gallery import Gallery
from resource.pom.keyboard import Keyboard
from utils.manager import MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.photo1, app.photo_dir)
    device.hdc_file_send(app.photo2, app.photo_dir)
    device.hdc_file_send(app.photo3, app.photo_dir)
    device.mediatool_send(app.photo_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击右上角“+”新建相册')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        # 照片
        tab_photos = device.get_element_by_key('TabPhotos')
        # 相册
        tab_albums = device.get_element_by_key('TabAlbums')
        device.click_element(tab_albums)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('ActionBarButtonNew'))
        logging.info('预期结果1.进入相册编辑界面')
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_key_exist('NewAlbumDialogTextInput')
        device.assert_text_exist('取消')
        device.assert_text_exist('确定')

        logging.info('步骤2.相册名称输入')
        new_album_name = device.get_element_by_key('NewAlbumDialogTextInput').get('text')
        device.click_element(device.get_element_by_key('NewAlbumDialogTextInput'))
        logging.info('预期结果2.唤醒键盘可以正常输入相册名称')
        device.refresh_layout()
        device.assert_soft_keyboard_on()
        # 输入一个a
        device.click(*Keyboard.key_a)

        logging.info('步骤3.点击确认')
        device.click_element(device.get_element_by_text('确定'))
        logging.info('预期结果3.进入选择图片/视频界面')
        device.refresh_layout()
        device.assert_text_exist('选择图片')
        device.assert_key_exist('SelectAlbum0')

        logging.info('步骤4.选择图片/视频')
        device.click_element(device.get_element_by_key('SelectAlbum0'))
        device.refresh_layout()
        img0 = device.get_element_by_key('ImageGridFocus_file', index=0, rule=MatchRules.Contain)
        img1 = device.get_element_by_key('ImageGridFocus_file', index=1, rule=MatchRules.Contain)
        img2 = device.get_element_by_key('ImageGridFocus_file', index=2, rule=MatchRules.Contain)
        check_box0_before = device.get_element_by_type('Checkbox', index=0)
        check_box1_before = device.get_element_by_type('Checkbox', index=1)
        check_box2_before = device.get_element_by_type('Checkbox', index=2)
        device.click_element(check_box0_before)
        device.click_element(check_box1_before)
        device.click_element(check_box2_before)
        logging.info('预期结果4.可以正常勾选一个或多个图片/视频')
        device.refresh_layout()
        check_box0_after = device.get_element_by_type('Checkbox', index=0)
        check_box1_after = device.get_element_by_type('Checkbox', index=1)
        check_box2_after = device.get_element_by_type('Checkbox', index=2)
        assert check_box0_before.get('checked') != check_box0_after.get('checked'), '勾选异常'
        assert check_box1_before.get('checked') != check_box1_after.get('checked'), '勾选异常'
        assert check_box2_before.get('checked') != check_box2_after.get('checked'), '勾选异常'

        logging.info('步骤5.勾选完成后点击右上角√保存')
        device.click_element(device.get_element_by_key('ActionBarButtonOK'))
        logging.info('预期结果5.保存成功，在相册界面显示新建的相册名称，里面包含勾选的图片/视频')
        device.refresh_layout()
        assert device.get_element_by_key('SingleTitleText').get('text') == new_album_name + 'a', '相册名称命令失败'
        img0_1 = device.get_element_by_key('ImageGridFocus_file', index=0, rule=MatchRules.Contain)
        img1_1 = device.get_element_by_key('ImageGridFocus_file', index=1, rule=MatchRules.Contain)
        img2_1 = device.get_element_by_key('ImageGridFocus_file', index=2, rule=MatchRules.Contain)
        assert img0_1, '添加相册失败'
        assert img1_1, '添加相册失败'
        assert img2_1, '添加相册失败'
